package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.Administrators;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/*
* 管理员业务
*
* 1.登录业务
* 2.修改密码
*
* */
@RestController
@RequestMapping("/admin")
public class AdministratorController {
    @Autowired
    private AdministratorService administratorService;
    /**登录业务
     * @description:
     * @author:
     * @date: 2024/3/7 下午 04:03
     * @param:
     * @return:
     **/
    @PostMapping("/login")
    public Result login(@RequestBody Map<String,Object> requestBody){
        String email = (String) requestBody.get("email");
        String password = (String) requestBody.get("password");
        System.out.println(email);
        System.out.println(password);
        Administrators admins = administratorService.login(email,password);
        if(admins!=null){
            return new Result(true,admins);
        }else{
            return new Result(false,"没有查询到相关信息");
        }

    }
    /*
     * 2.修改密码
     * */
    @PutMapping("/updatePwd")
    public Result changePwd(@RequestBody Map<String,Object> requestBody){
        Integer uid = (Integer) requestBody.get("uid");
        String newPassword = (String) requestBody.get("password");

        Integer result = administratorService.updateAdminPassword(uid, newPassword);
        return new Result(result>0?true:false,result>0?"修改成功":"修改失败");
    }
}
